﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SIMS.Global;
using SIMS.Database;
using System.Collections;
using System.Data;

namespace SIMS.Student
{
    public class StudentTimeTable
    {
        public int Id { get; set; }
        public int CompanyID { get; set; }
        public string CompanyName { get; set; }

        public int RoomID { get; set; }
        public string RoomName { get; set; }

        public int CourseID { get; set; }
        public string CourseName { get; set; }

        public int CourseDetailID { get; set; }
        public string CourseDetailName { get; set; }

        public int StandardLevelID { get; set; }
        public string StandardLevelName { get; set; }

        public int MajorFieldID { get; set; }
        public string MajorFieldName { get; set; }

        public string Name { get; set; }
        public string Description { get; set; }

        public bool IsActive { get; set; }
        public DateTime CreatedDate { get; set; }
        public DateTime ModifiedDate { get; set; }

        public MyError Error { get; set; }
    }

    public class StudentTimeTableList : List<StudentTimeTable>
    {
        public MyError Error { get; set; }
        public StudentTimeTableList()
        {
        }
    }

    public class StudentTimeTableDB
    {
        MyDatabase _db = new MyDatabase();

        public StudentTimeTable Save(StudentTimeTable StudentTimeTable)
        {
            try
            {
                SortedList _param = new SortedList();
                _param.Add("@Id", StudentTimeTable.Id);
                _param.Add("@CourseDetailId", StudentTimeTable.CourseDetailID);
                _param.Add("@RoomId", StudentTimeTable.RoomID);

                if (StudentTimeTable.Description != string.Empty)
                {
                    _param.Add("@Description", StudentTimeTable.Description);
                }
                else
                {
                    _param.Add("@Description", DBNull.Value);
                }

                _param.Add("@Name", StudentTimeTable.Name);


                _param.Add("@IsActive", StudentTimeTable.IsActive);

                int _affRow = _db.ExecuteNonSqlSP("usp_StudentTimeTableInsertUpdateSingleItem", _param);

                if (_affRow > 0)
                {
                    StudentTimeTable.Error.ErrorNo = 0;
                }
                else
                {
                    StudentTimeTable.Error.ErrorNo = 1;
                    StudentTimeTable.Error.ErrorMessage = "No row affect when saving school's course details.";
                }
            }
            catch (Exception Ex)
            {
                StudentTimeTable.Error.ErrorNo = 10002;
                StudentTimeTable.Error.ErrorLocation = "Course";
                StudentTimeTable.Error.ErrorMessage = Ex.Message;
                StudentTimeTable.Error.ErrorStack = Ex.StackTrace;

                MyError.CreateExceptionLog(Ex, "Course");
            }

            return StudentTimeTable;
        }

        public StudentTimeTableList GetStudentTimeTableList(int CompanyId)
        {
            StudentTimeTableList retValue = new StudentTimeTableList();

            try
            {
                DataTable _dt = new DataTable("tblCourse");

                SortedList _param = new SortedList();
                _param.Add("@CompanyId", CompanyId);
                _dt = _db.GetDataTableBySP("usp_StudentTimeTableSelectItemList", _param);

                if (_dt != null && _dt.Rows.Count > 0)
                {
                    StudentTimeTable _StudentTimeTable = new StudentTimeTable();
                    foreach (DataRow _dr in _dt.Rows)
                    {
                        FillData(_dr, _StudentTimeTable);
                        retValue.Add(_StudentTimeTable);
                    }
                }
            }
            catch (Exception Ex)
            {
                retValue.Error = new MyError();
                retValue.Error.ErrorNo = 10001;
                retValue.Error.ErrorLocation = "Course";
                retValue.Error.ErrorMessage = Ex.Message;
                retValue.Error.ErrorStack = Ex.StackTrace;

                MyError.CreateExceptionLog(Ex, "Course");
            }

            return retValue;
        }

        public StudentTimeTable GetStudentTimeTable(int Id)
        {
            StudentTimeTable retValue = new StudentTimeTable();

            try
            {
                DataTable _dt = new DataTable("tblCourse");

                SortedList _param = new SortedList();
                _param.Add("@Id", Id);
                _dt = _db.GetDataTableBySP("usp_StudentTimeTableSelectSingleItem", _param);

                if (_dt != null && _dt.Rows.Count > 0)
                {
                    FillData(_dt.Rows[0], retValue);
                }
            }
            catch (Exception Ex)
            {
                retValue.Error = new MyError();
                retValue.Error.ErrorNo = 10001;
                retValue.Error.ErrorLocation = "Course";
                retValue.Error.ErrorMessage = Ex.Message;
                retValue.Error.ErrorStack = Ex.StackTrace;

                MyError.CreateExceptionLog(Ex, "Course");
            }

            return retValue;
        }

        private void FillData(DataRow aRow, StudentTimeTable StudentTimeTable)
        {
            StudentTimeTable.Error = new MyError();
            StudentTimeTable.Id = Convert.ToInt32(aRow["Id"].ToString());
            StudentTimeTable.CourseDetailID = Convert.ToInt32(aRow["CourseDetailId"].ToString());
            StudentTimeTable.RoomID = Convert.ToInt32(aRow["RoomId"].ToString());
            StudentTimeTable.CompanyID = Convert.ToInt32(aRow["CompanyID"].ToString());
            StudentTimeTable.CourseID = Convert.ToInt32(aRow["CourseId"].ToString());
            StudentTimeTable.MajorFieldID = Convert.ToInt32(aRow["MajorFieldID"].ToString());
            StudentTimeTable.StandardLevelID = Convert.ToInt32(aRow["StandardLevelID"].ToString());

            if (aRow["Description"] != DBNull.Value)
            {
                StudentTimeTable.Description = aRow["Description"].ToString();
            }
            else
            {
                StudentTimeTable.Description = string.Empty;
            }

            StudentTimeTable.Name = aRow["Name"].ToString();
            StudentTimeTable.CourseDetailName = aRow["CourseDetailName"].ToString();
            StudentTimeTable.RoomName = aRow["RoomName"].ToString();
            StudentTimeTable.CompanyName = aRow["CompanyName"].ToString();
            StudentTimeTable.StandardLevelName = aRow["StandardLevelName"].ToString();
            StudentTimeTable.MajorFieldName = aRow["MajorFieldName"].ToString();
            StudentTimeTable.CourseName = aRow["CourseName"].ToString();

            StudentTimeTable.IsActive = Convert.ToBoolean(aRow["IsActive"].ToString());

            StudentTimeTable.Error.ErrorNo = 0;
        }
    }
}

